<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in rolesList" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="doSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from "@/api/settings";
import { getStaffInfo } from "@/api/user";
import { assignRoles } from "@/api/permissions";

export default {
  props: {
    curId: {
      type: String,
    },
  },
  data() {
    return {
      roleIds: [],
      rolesList: [],
    };
  },
  created() {
    this.loadRoles();
  },
  methods: {
    closeDialog() {
      this.$emit("closeRoleDialog");
    },
    async loadRoles() {
      try {
        const roleListRes = await getRoles({ page: 1, pagesize: 1000 });
        // console.log(res);
        this.rolesList = roleListRes.data.rows;
        const roleIds = await getStaffInfo(this.curId);
        console.log(roleIds.data);
        this.roleIds = [...roleIds.data.roleIds];
      } catch (err) {
        console.dir(err);
      }
    },
    doSubmit() {
      this.doAssign();
    },
    async doAssign() {
      try {
        const res = await assignRoles(this.curId, this.roleIds);
        console.log(res);
        this.$message.success(res.message);
        this.$emit("success");
      } catch (err) {
        this.$message.error(err.message);
      }
    },
  },
};
</script>